package org.jf.tool.fx.jftoolfx.encrypt.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import org.jf.tool.fx.jftoolfx.common.BaseController;
import org.jf.tool.fx.jftoolfx.util.AsymmetricUtil;
import org.jf.tool.fx.jftoolfx.util.SymmetricUtil;

import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HexFormat;
import java.util.ResourceBundle;

/**
 * 非对称加解密
 *
 * @author jifeng_zhang
 * @date 2025/8/18 下午2:16
 */
public class AsymmetricController extends BaseController {
    @FXML
    public ToggleGroup typeGroup;
    @FXML
    public ToggleGroup plainGroup;
    @FXML
    public TextArea plainText;
    @FXML
    public TextField filePathField;
    @FXML
    public HBox fileBox;
    @FXML
    public ToggleGroup resultGroup;
    @FXML
    public TextArea resultArea;

    @FXML
    public TextArea publicKeyTextArea;

    @FXML
    public TextArea privateKeyTextArea;


    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {

    }

    public void encrypt(ActionEvent actionEvent) {
        String publicKey = publicKeyTextArea.getText();
        String plainType = plainGroup.getSelectedToggle().getUserData().toString();
        String plainStr = plainText.getText();
        byte[] plain = plain2Byte(plainType, plainStr);
        try {
            if (publicKey == null || publicKey.isEmpty()) {
                resultArea.setText("请输入公钥！");
                return;
            }
            if (plainStr == null || plainStr.isEmpty()) {
                resultArea.setText("请输入原文！");
                return;
            }
            byte[] result = AsymmetricUtil.encrypt(AsymmetricUtil.TYPE.RSA, publicKey, plain);
            String resultType = resultGroup.getSelectedToggle().getUserData().toString();
            if ("base64".equalsIgnoreCase(resultType)) {
                resultArea.setText(Base64.getEncoder().encodeToString(result));
            } else if ("hex".equalsIgnoreCase(resultType)) {
                resultArea.setText(HexFormat.of().formatHex(result).toUpperCase());
            } else {
                resultArea.setText(new String(result));
            }
        } catch (Exception e) {
            resultArea.setText("加密失败：" + e.getMessage());
        }
    }

    public void decrypt(ActionEvent actionEvent) {

    }

    public void clear(ActionEvent actionEvent) {
        plainText.setText("");
        resultArea.setText("");
    }

    public void copy(ActionEvent actionEvent) {
        copy(resultArea.getText());
    }

    public void upload(ActionEvent actionEvent) {
    }


    public byte[] plain2Byte(String type, String plain) {
        if ("Base64".equalsIgnoreCase(type)) {
            return Base64.getDecoder().decode(plain);
        } else if ("Hex".equalsIgnoreCase(type)) {
            return HexFormat.of().parseHex(plain);
        }
        return plain.getBytes(StandardCharsets.UTF_8);
    }
}
